package ComPuter.entity;

import commons.entity.CpMissonResults;
import commons.entity.CurrentDateTime;
import commons.entity.NativeCp;
import commons.entity.TrainResults;
import commons.util.GameUtil;
import commons.util.MySpringUtil;
import commons.util.ProcessDealUtil;
import fgoScript.entity.panel.FgoFrame;
import fgoScript.service.TimerManager;
import gudaziEx.util.ChainUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.swing.*;
import java.awt.*;

public class ComputerChain {
    private static final Logger LOGGER = LogManager.getLogger(ComputerChain.class);
    private Point mousePosition;

    private int outMinutes;
    private int checkPerMinuters;
    private int sleepOrShutDown;
    private int sleepInSeconds;

    public int getSleepInSeconds() {
        return sleepInSeconds;
    }

    public void setSleepInSeconds(int sleepInSeconds) {
        this.sleepInSeconds = sleepInSeconds;
    }

    public int getSleepOrShutDown() {
        return sleepOrShutDown;
    }

    public void setSleepOrShutDown(int sleepOrShutDown) {
        this.sleepOrShutDown = sleepOrShutDown;
    }

    private boolean ifGoCheckPoint;

    public Point getMousePosition() {
        return mousePosition;
    }

    public void setMousePosition(Point mousePosition) {
        this.mousePosition = mousePosition;
    }

    public int getOutMinutes() {
        return outMinutes;
    }

    public void setOutMinutes(int outMinutes) {
        this.outMinutes = outMinutes;
    }

    public int getCheckPerMinuters() {
        return checkPerMinuters;
    }

    public void setCheckPerMinuters(int checkPerMinuters) {
        this.checkPerMinuters = checkPerMinuters;
    }

    public boolean isIfGoCheckPoint() {
        return ifGoCheckPoint;
    }

    public void setIfGoCheckPoint(boolean ifGoCheckPoint) {
        this.ifGoCheckPoint = ifGoCheckPoint;
    }

    public static void GoCheckRest(){

        ComputerChain cc;
        Point tempPoint;
        Point nowPoint;
//        double px = nowPoint.getX();
//        double py = nowPoint.getY();  cc.outHours*3600*1000
        boolean ifGo=true;
        cc = ChainUtil.getComPuterChain();
        for (int i = 0; ChainUtil.getComPuterChain().ifGoCheckPoint ; i++) {
            LOGGER.info("启动节能模式！ 次数： " + i+1);
            tempPoint = GameUtil.getMousePosition();
            for (int j = 0; cc.ifGoCheckPoint && (j+=cc.checkPerMinuters*60*1000) < cc.outMinutes*60*1000; ) {
                GameUtil.delay(cc.checkPerMinuters*60*1000);
                LOGGER.info("检测：" + j + " 小于 " + cc.outMinutes*60*1000);
                nowPoint = GameUtil.getMousePosition();
                if(Math.abs(nowPoint.getY()-tempPoint.getY()) > 10  ||
                        Math.abs(nowPoint.getX() - tempPoint.getX()) >10){
                    j=0;
                    tempPoint = GameUtil.getMousePosition();
                    LOGGER.info("发现鼠标移动 重新进行计时！");
                }
                LOGGER.info("当前点坐标： " + nowPoint.getX() + " _ " + nowPoint.getY());
                cc = ChainUtil.getComPuterChain();
            }
            if(cc.ifGoCheckPoint){
                nowPoint = GameUtil.getMousePosition();
                if(Math.abs(nowPoint.getY()-tempPoint.getY()) < 10  ||
                        Math.abs(nowPoint.getX() - tempPoint.getX()) < 10){
                    //启动睡眠、关机
                    if(cc.getSleepOrShutDown() == 0){
                        String showText = "计算机将在"+ GameUtil.ConvertSecondsToHMS(cc.getSleepInSeconds())+"后进入休眠模式！";
                        LOGGER.info(showText);
                        ProcessDealUtil.displayTray("休眠",showText, TrayIcon.MessageType.INFO);
                        ClassPathXmlApplicationContext applicationContext = MySpringUtil.getApplicationContext();
                        FgoFrame f = (FgoFrame) applicationContext.getBean("fgoFrame");
                        f.setVisible(true);
                        f.showSystemPage();
                        ProcessDealUtil.sleepComputerIntime(cc.getSleepInSeconds());
                    }else{
                        ProcessDealUtil.closeComputerInTime(1);
                        LOGGER.info("计算计1分钟后关机！");
                    }
                    GameUtil.delay(60*1000);
                }else{
                    LOGGER.info("鼠标已移动！不进行操作！");
                }
            }

            cc = ChainUtil.getComPuterChain();
        }
        LOGGER.info("节能模式已结束！");
        
    }
    public static void GoCheckTopToon(){

        TrainResults tr = TrainResults.getInstance();

        boolean dateRight= GameUtil.getNowDateString().equals(tr.getToptoonSignDate());

        boolean signRight = tr.isToptoon01Finish()
                && tr.isToptoon02Finish()
                && tr.isToptoon03Finish()
                && tr.isToptoon04Finish();
        if(CurrentDateTime.getCurrentHour() > 3 ){
            if(dateRight && signRight ){
                if(tr.isGoToptoonReport()){
                    String result = "TopToon01:" + (tr.isToptoon01Finish() == true ? "完成":"失败") +"  "
                            + "TopToon02:" + (tr.isToptoon02Finish() == true ? "完成":"失败") +"\r\n"
                            + "TopToon03:" + (tr.isToptoon03Finish() == true ? "完成":"失败")+"  "
                            + "TopToon04:" + (tr.isToptoon04Finish() == true ? "完成":"失败");
                    ProcessDealUtil.displayTray("签到成功",result, TrayIcon.MessageType.INFO);
                    tr.setGoToptoonReport(false);
                    ChainUtil.saveTrainResults(tr);
                }

            }else {
                String result ="";

                if(tr.getToptoonSignDate().equals(GameUtil.getNowDateString())){
                    result = "TopToon01 : " + (tr.isToptoon01Finish() == true ? "完成":"失败") +"  "
                            + "TopToon02 : " + (tr.isToptoon02Finish() == true ? "完成":"失败") +"\r\n"
                            + "TopToon03 : " + (tr.isToptoon03Finish() == true ? "完成":"失败")+"  "
                            + "TopToon04 : " + (tr.isToptoon04Finish() == true ? "完成":"失败");
                    ProcessDealUtil.displayTray("签到已执行,但不完整",result, TrayIcon.MessageType.ERROR);
                }else{
                    result = "最后一次签到时间 ：" + tr.getToptoonSignDate();
                    ProcessDealUtil.displayTray("签到未执行",result, TrayIcon.MessageType.ERROR);
                }

            }
        }

    }
    public static void GoCheckCpRunningState(){

        CpMissonResults cr = CpMissonResults.getInstance();

        boolean dateRight= GameUtil.getNowDateString().equals(cr.getCpRunningDate());
        boolean signRight = true;
        boolean trainRight = true;
        boolean rewardRight = true;
        int size = cr.getAccountRewardState().length;
        boolean[] accountSignState = cr.getAccountSignState();

        boolean[] accountTrainState = cr.getAccountTrainState();

        boolean[] accountRewardState = cr.getAccountRewardState();

        boolean cpRunningState =cr.isCpRunningState();

        for (int i = 0; i < size; i++) {
            if(accountSignState[i] == false){
                signRight = false;
                break;
            }
        }
        size = GameUtil.strToIntArray(GameUtil.getValueFromConfig("FgoArray"), false).length;
        for (int i = 0; i < size; i++) {
            if(accountTrainState[i] == false){
                trainRight = false;
                break;
            }
        }
        size = GameUtil.strToIntArray(GameUtil.getValueFromConfig("FgoRewardArray"), false).length;
        for (int i = 0; i < size; i++) {
            if(accountRewardState[i] == false){
                rewardRight = false;
                break;
            }
        }

        boolean checkRight =   dateRight
                            && signRight
                            && trainRight
                            && rewardRight;
        if(CurrentDateTime.getCurrentHour() > 3 ){

            String result ="";
            result += "签到是否完成 : " + (signRight == true ? "完成":"失败") +" ";

            result += "电脑开关状态 : " + (cpRunningState == true ? "开机":"关机")+"\r\n";

            result +="训练详情 : "+"\r\n";
            for (int i = 0; i < size; i++) {
                result += ((i+1+"").length()==1 ? "0"+(i+1) : ""+(i+1)) + ": " + (accountTrainState[i] == true ? "✔" : "✖") + "  ";
                if((i+1)%6 == 0 ){
                    result += "\r\n";
                }
            }
            result +="\r\n";

            result +="奖励详情 : "+"\r\n";
            for (int i = 0; i < size; i++) {
                result += ((i+1+"").length()==1 ? "0"+(i+1) : ""+(i+1)) + ": " + (accountRewardState[i] == true ? "✔" : "✖") + "  ";
                if((i+1)%6 == 0 ){
                    result += "\r\n";
                }
            }

            if(dateRight && checkRight ){
                if(cr.isCpStateReport()){
                    ProcessDealUtil.displayTray("FGO任务成功",result, TrayIcon.MessageType.INFO);
                    cr.setCpStateReport(false);
                    ChainUtil.saveCpMissonResults(cr);
                }

            }else {
                if(cr.getCpRunningDate().equals(GameUtil.getNowDateString())){
                    ProcessDealUtil.displayTray("任务执行中，未完成",result, TrayIcon.MessageType.ERROR);
                }else{
                    result = "最后一次任务执行时间 ：" + cr.getCpRunningDate();
                    ProcessDealUtil.displayTray("任务未执行",result, TrayIcon.MessageType.ERROR);
                }

            }
        }

    }

    public static void main(String[] args) {
        System.currentTimeMillis();
    }
}
